<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript">
       $(function () {
           var str="<option value='0'>请选择</option>";
           $.ajax({
               url:"getClasses",
               success:function (data) {
                $(data).each(function () {
                   str+="<option value='"+this.id+"'>"+this.name+"</option>";
                })
                 $("#select").empty().append(str);
               }
           })

       })


       function ti() {
            var name = $("#name").val();
            var age = $("#age").val();
            var gender = $("#gender").val();
            var telephone = $("#telephone").val();
            var regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            if ($("#email").val()==""){
                alert("请输入电子邮件");
                return false;
            }else if ($("#email").val()!=regEx1){
                alert("请输入正确的电子邮件");
                return false;
            }else {
                var email = $("#email").val();
            }

            if ($("#select").val()!=0){
                var classId = $("#select").val();
            } else {
                alert("请选择班级")
                return false;
            };

            $.ajax({
                url:"/add",
                data:{"name":name,"age":age,"gender":gender,"telephone":telephone,"email":email,"classId":classId},
                success:function (result) {
                    if (result){
                        alert("添加成功");
                        window.location.href="index.html";
                    } else {
                        alert("添加失败")
                    }
                }
            })
        }
    </script>
</head>
<body>
<h2>学员信息添加</h2>
<table>
    <tr><td>姓名</td><td><input type="text" id="name" name="name"></td></tr>
    <tr><td>性别</td><td><input type="text" id="gender" name="gender"></td></tr>
    <tr><td>年龄</td><td><input type="text" id="age" name="age"></td></tr>
    <tr><td>电话</td><td><input type="text" id="telephone" name="telephone"></td></tr>
    <tr><td>email</td><td><input type="email" id="email" name="email"></td></tr>
    <tr><td>班级</td><td><select id="select"></select></td></tr>
    <tr><td><input type="button" value="增加" onclick="ti()"></td>
    <td><input type="button" value="返回" onclick="window.history.go(-1)"></td></tr>
</table>
</body>
</html>